import { smsSend } from '@/static/js/login'
export default {
	data() {
		return {
			form: {
				mobile: "", //	是	string	无
				captcha: "", //	是	string	发送验证码
				newpassword: "", //是	string	无
				newpassword2: "", //	是	string	无
			},
			countDown: 60,//倒计时
		};
	},
	methods: {
		//发送验证码
		sendCode() {
			if (this.countDown < 60) {
				return false;
			}
			var form = this.form;
			if (form.mobile == "") {
				this.$tools.showToast("请输入手机号");
				return false;
			}
			var data = {
				mobile: form.mobile, //	是	string	手机号
				event: "changepwd", //	是	string	事件:register=注册,mobilelogin=验证码登录,changemobile=更改手机号,resetpwd=找回密码
			}
			smsSend(data).then(res => {
					if (res.code == 1) {
						this.$tools.showToast("短信发送成功");
						clearInterval(this.countDown);
						this.countDown = setInterval(res => {
							if (this.countDown - 1 <= 0) {
								this.countDown = 60;
								clearInterval(this.countDown);
							} else {
								this.countDown = this.countDown - 1
							}
						}, 1000)
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch(err => {
					console.log('错误回调', err);
				});
		},

		//校验
		checkOut() {
			if (this.form.mobile == "") {
				this.$tools.showToast("请输入手机号");
				return false;
			}
			if (this.form.captcha == "") {
				this.$tools.showToast("请输入验证码");
				return false;
			}
			if (this.form.newpassword == "") {
				this.$tools.showToast("请输入密码");
				return false;
			}
			if (this.form.newpassword2 == "") {
				this.$tools.showToast("请再次确认密码");
				return false;
			}
			return true
		},
		//提交
		submit() {
			if (!this.checkOut()) {
				return false;
			}
			var data = this.form;
			changePwd(data).then(res => {
				if (res.code == 1) {
					this.$tools.showToast("密码修改成功");
					setTimeout(res => {
						uni.reLaunch({
							url: "../login/login"
						})
					}, 1000)
				} else {
					this.$tools.showToast(res.msg);
				}
			})
				.catch(err => {
					console.log('错误回调', err);
				});
		},
	},

}